<template>
  <div class="login-container">
    <div class="login-background"></div>
    <el-card class="login-card">
      <template #header>
        <h2 class="login-title">会议室预订系统</h2>
      </template>
      <el-form :model="loginForm" :rules="rules" ref="loginFormRef" label-width="80px">
        <el-form-item label="账号" v-if="loginForm.role !== 'admin'">
          <el-input v-model="loginForm.username" placeholder="请输入用户名"></el-input>
        </el-form-item>
        <el-form-item label="密码" prop="password">
          <el-input v-model="loginForm.password" type="password" placeholder="请输入密码" @keyup.enter="handleLogin"></el-input>
        </el-form-item>
        <el-form-item label="身份" prop="role">
          <el-select v-model="loginForm.role" placeholder="请选择身份" style="width: 100%">
            <el-option label="客户" value="customer"></el-option>
            <el-option label="员工" value="staff"></el-option>
            <el-option label="管理员" value="admin"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="handleLogin" :loading="loading" style="width: 100%">登录</el-button>
        </el-form-item>
        <el-form-item v-if="loginForm.role === 'customer'">
          <el-button @click="goToRegister" style="width: 100%">注册账号</el-button>
        </el-form-item>
      </el-form>
    </el-card>
  </div>
</template>

<script setup>
import { ref, watch } from 'vue'
import { useRouter } from 'vue-router'
import { ElMessage } from 'element-plus'
import { setSessionStorage,getSessionStorage } from '../../utils/common'
import { userLogin, employeeLogin } from '../../api/authApi'

const router = useRouter()
const loading = ref(false)
const loginFormRef = ref(null)

const loginForm = ref({
  username: '',
  password: '',
  role: ''
})

const rules = {
  password: [
    { required: true, message: '请输入密码', trigger: 'blur' },
    { min: 6, message: '密码长度不能小于6位', trigger: 'blur' }
  ],
  role: [
    { required: true, message: '请选择身份', trigger: 'change' }
  ]
}

// 监听角色变化
watch(() => loginForm.value.role, (newRole) => {
  if (newRole === 'admin') {
    loginForm.value.username = 'admin'
  } else {
    loginForm.value.username = ''
  }
})

const handleLogin = async () => {
  if (!loginFormRef.value) return
  
  try {
    await loginFormRef.value.validate()
    loading.value = true

    let res
    const params = {
      name: loginForm.value.username,
      loginPassword: loginForm.value.password
    }

    switch(loginForm.value.role) {
      case 'customer':
        res = await userLogin(params)
        if (res.flag) {
          setSessionStorage("username", res.result.name)
          setSessionStorage("customerId", res.result.userId)
          router.push('/homeCustomer')
        } else {
          ElMessage.error(res.reason || '登录失败')
        }
        break
      case 'staff':
        res = await employeeLogin(params)
        if (res.flag) {
          setSessionStorage("username", res.result.name)
          setSessionStorage("employeeId", res.result.employeeId)
          router.push('/homeStaff')
        } else {
          ElMessage.error(res.reason || '登录失败')
        }
        break
      case 'admin':
        // 管理员固定ID为1
        params.name = "admin"
        res = await employeeLogin(params)
        if (res.flag) {
          setSessionStorage("username", "管理员")
          setSessionStorage("adminId", 1)
          router.push('/homeAdmin')
        } else {
          ElMessage.error(res.reason || '登录失败')
        }
        break
    }
  } catch (error) {
    console.error('登录错误：', error)
    ElMessage.error('登录失败，请检查输入')
  } finally {
    loading.value = false
  }
}

const goToRegister = () => {
  router.push('/register')
}
</script>

<style scoped>
.login-container {
  height: 100vh;
  display: flex;
  justify-content: center;
  align-items: center;
  position: relative;
  overflow: hidden;
}

.login-background {
  position: absolute;
  top: 0;
  left: 0;
  right: 0;
  bottom: 0;
  background: linear-gradient(135deg, #1890ff 0%, #36cfc9 100%);
  z-index: 0;
}

.login-background::before {
  content: '';
  position: absolute;
  top: 0;
  left: 0;
  right: 0;
  bottom: 0;
  background: url('data:image/svg+xml,<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 1440 320"><path fill="rgba(255,255,255,0.1)" fill-opacity="1" d="M0,96L48,112C96,128,192,160,288,160C384,160,480,128,576,112C672,96,768,96,864,112C960,128,1056,160,1152,160C1248,160,1344,128,1392,112L1440,96L1440,320L1392,320C1344,320,1248,320,1152,320C1056,320,960,320,864,320C768,320,672,320,576,320C480,320,384,320,288,320C192,320,96,320,48,320L0,320Z"></path></svg>') no-repeat bottom;
  background-size: cover;
  opacity: 0.6;
}

.login-card {
  width: 400px;
  position: relative;
  z-index: 1;
  background: rgba(255, 255, 255, 0.9);
  backdrop-filter: blur(10px);
  border: none;
  box-shadow: 0 8px 32px rgba(0, 0, 0, 0.1);
}

.login-title {
  color: #1890ff;
  margin: 0;
  font-size: 24px;
  font-weight: 600;
}

.login-card :deep(.el-card__header) {
  text-align: center;
  background: rgba(255, 255, 255, 0.8);
  border-bottom: 1px solid rgba(0, 0, 0, 0.1);
}

.login-card :deep(.el-input__wrapper) {
  background: rgba(255, 255, 255, 0.8);
}

.login-card :deep(.el-button--primary) {
  background: #1890ff;
  border: none;
  height: 40px;
  font-size: 16px;
  transition: all 0.3s ease;
}

.login-card :deep(.el-button--primary:hover) {
  transform: translateY(-2px);
  box-shadow: 0 4px 12px rgba(24, 144, 255, 0.4);
}

.login-card :deep(.el-button--default) {
  border: 1px solid #1890ff;
  color: #1890ff;
  height: 40px;
  font-size: 16px;
  transition: all 0.3s ease;
}

.login-card :deep(.el-button--default:hover) {
  background: rgba(24, 144, 255, 0.1);
}
</style> 